package com.tulingxueyuan.mall.controller;


import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.AddCarDTO;
import com.tulingxueyuan.mall.dto.CartItemStockDTO;
import com.tulingxueyuan.mall.modules.oms.model.OmsCartItem;
import com.tulingxueyuan.mall.modules.oms.service.OmsCartItemService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car")
@Api(tags = "CarController" ,description = "购物车")
public class CarController {


    @Autowired
    OmsCartItemService cartItemService;
    /**
     * .post("/car/add", {
     *           productId: this.id,
     *           productSkuId: this.skuId,
     *           quantity: 1,
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody AddCarDTO addCarDTO) {
        // TODO: 2019/5/29 购物车添加
        boolean result = cartItemService.add(addCarDTO);
        if (result) {
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }

    /**
     * 初始化状态栏的购物车商品数量
     *  this.axios.get('/car/products/sum').then((res=0)=>{
     *         // 存全局变量
     *         this.$store.dispatch('saveCartCount',res);
     *       })
     */
    @RequestMapping(value = "/products/sum", method = RequestMethod.GET)
    public CommonResult getCartCount() {

        Integer count = cartItemService.getCartCount();


        return CommonResult.success(count);
    }

    /**
     * 获取购物车数据初始化页面
     * this.axios.get('/car/list').then((res)=>{
     *           this.renderData(res);
     *         })
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult getCartList() {

        List<CartItemStockDTO> list = cartItemService.getCartList();
        return CommonResult.success(list);
    }

    /**
     * 更新商品的数量
     *  this.axios.post('/car/update/quantity',Qs.stringify({
     *             id:item.id,
     *             quantity:item.quantity
     *           })
     */
    @RequestMapping(value = "/update/quantity", method = RequestMethod.POST)
    public CommonResult updateQuantity(@RequestParam Long id,@RequestParam Integer quantity) {

        boolean result = cartItemService.updateQuantity(id,quantity);
        if (result) {
            return CommonResult.success(result);
        }else {

            return CommonResult.failed();
        }


    }
    /**
     * 删除购物车商品
     *  this.axios.post('/car/delete',Qs.stringify({
     *             ids:item.id
     *           })
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestParam Long ids) {
        boolean result = cartItemService.delete(ids);
        if (result) {
            return CommonResult.success(result);
        }else {

            return CommonResult.failed();
        }

    }

}
